http://huxuemail.web-104.com
http://www.cnblogs.com/wuniaoheart/
数据库中,存放文章的表中有“Tag”字段,用来存放标签。标签之间用“,”分隔。比如“PHP,VB,随笔”。 下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现的次数为依据显示出不同大小的文字连接。 其中的细节,不做解释了! 观念陈、方法笨、效率低的标签云的实现代码如下:
1 ";22 $Tags = explode(",",$TagString);23 $n = 1;24 $i = 0;25 $Continue = TRUE;26 //echo $Tags[1]."";27 //in case no-label's article28 while($Tags[$n] OR $Tags[++$n] OR $Tags[++$n] ){29 $EachTag = $Tags[$n++];30 //echo $EachTag."";31 $Continue = TRUE;32 for($i=0;$Continue;$i++){33 if( $EachTagStr[$i][0] ) {34 if( $EachTagStr[$i][0] == $EachTag ){35 $EachTagStr[$i][1]++;36 $Continue = FALSE;37 }38 else {39 if( $EachTagStr[$i+1][0] ) $Continue = TRUE;40 else {41 $EachTagStr[$i+1][0] = $EachTag;42 $EachTagStr[$i+1][1] = 1;43 $Continue = FALSE;44 }45 }46 } else { //initialize the array $EachTagStr[][]47 $EachTagStr[$i][0] = $EachTag;48 $EachTagStr[$i][1] = 1;49 $Continue = FALSE;50 }51 }52 }53 return $EachTagStr;54 }55 56 function ShowTag($Row,$ablink){57 $i = 0;58 while($Row[$i][0]){59 $EachTag = $Row[$i][0];60 $EachCount = $Row[$i][1];61 $Size = SetSize($EachCount);62 echo " ".$EachTag."(".$EachCount.")"." ";63 $i++;64 }65 }66 67 function GetTag(){68 $QuerySet = mysql_query("select * from article");69 while($Row = mysql_fetch_array($QuerySet)){70 $Tag = $Row['tag'];71 $TagString = $TagString.",".$Tag;72 }73 return $TagString;74 }75 76 function SetSize($Size){77 $Size += 10;78 if($Size > 30)79 $Size = 30;80 return $Size;81 }82 83 //Go 84 echo "";85 echo "";90 91 ?>标签云
";86 $String = GetTag();87 $Row = CountTag($String);88 ShowTag($Row,$ablink);89 echo "
OK,DONE!